<?php
/**
 * @title 2905. 找出满足差值条件的下标 II - 堆
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $indexDifference
     * @param Integer $valueDifference
     * @return Integer[]
     */
    function findIndices($nums, $indexDifference, $valueDifference) {
        /**
         * @since 2024-02-01 rightMaxArray[j] = [max, maxIndex]
         * @since 2024-02-01 rightMinArray[j] = [min, minIndex]
         * @since 2024-02-01 表示下标j到数组尾部，最大值和对应下标
         * @since 2024-02-01 表示下标j到数组尾部，最小值和对应下标
         */
        $numsCount = count($nums);
        $rightMaxArray[$numsCount-1] = [$nums[$numsCount-1], $numsCount-1];
        $rightMinArray[$numsCount-1] = [$nums[$numsCount-1], $numsCount-1];

        /**
         * @since 2024-02-01 从右边开始遍历数组nums
         */
        for($j=$numsCount-2; $j>=$indexDifference; $j--){
            $num = $nums[$j];
            if($num > $rightMaxArray[$j+1][0]){
                $rightMaxArray[$j] = [$num, $j];
            } else {
                $rightMaxArray[$j] = $rightMaxArray[$j+1];
            }
            if($num < $rightMinArray[$j+1][0]){
                $rightMinArray[$j] = [$num, $j];
            } else {
                $rightMinArray[$j] = $rightMinArray[$j+1];
            }
        }

        /**
         * @since 2024-02-01 下标区间[0, i]的最大值和最小值
         */
        $max = $min = $nums[0];
        $maxIndex = $minIndex = 0;

        /**
         * @since 2024-02-01 从左边开始遍历数组nums
         */
        for($i=0, $j=$indexDifference; $j<$numsCount; $i++, $j++){
            $num = $nums[$i];
            if($num > $max){
                $max = $num;
                $maxIndex = $i;
            } else {}
            if($num < $min){
                $min = $num;
                $minIndex = $i;
            } else {}

            if(abs($max-$rightMinArray[$j][0]) >= $valueDifference){
                return [$maxIndex, $rightMinArray[$j][1]];
            } else {}
            if(abs($rightMaxArray[$j][0]-$min) >= $valueDifference){
                return [$minIndex, $rightMaxArray[$j][1]];
            } else {}
        }

        /**
         * @return [Integer, Integer]
         */
        return [-1, -1];
    }
}


/**
 * @url http://127.0.0.1/leetcode/202402/2024.02.01_4.php
 */
$datas = [
    [[5,1,4,1], 2, 4],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->findIndices(...$data);
    echo $xhprof->DumpArgs([$result]);
    // var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
